﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.purchaseTableAdapters;
using dhDataAcess.dataAcess.baobiaoDataSetTableAdapters ;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.produceLinq;
using dhDataAcess.produceDAL;
using dhDataAcess.sysDAL;

namespace dhSystemBLL.produceBLL
{
   public  class cgshrkBLL
    {
       private V_cgshrkDetailTableAdapter _V_cgshrkDetailAdapter = null;
       private V_cgshrkDetailTableAdapter V_cgshrkDetailAdapter
        {
            get
            {
                if (_V_cgshrkDetailAdapter == null) _V_cgshrkDetailAdapter = new V_cgshrkDetailTableAdapter();
                return _V_cgshrkDetailAdapter;

            }
        }
        private cgshrkMasterTableAdapter _cgshrkMasterlAdapter = null;
        private cgshrkMasterTableAdapter cgshrkMasterAdapter
        {
            get
            {
                if (_cgshrkMasterlAdapter == null) _cgshrkMasterlAdapter = new cgshrkMasterTableAdapter();
                return _cgshrkMasterlAdapter;

            }
        }
        private cgshrkDetailTableAdapter _cgshrkDetailAdapter = null;
        private cgshrkDetailTableAdapter cgshrkDetailAdapter
        {
            get
            {
                if (_cgshrkDetailAdapter == null) _cgshrkDetailAdapter = new cgshrkDetailTableAdapter();
                return _cgshrkDetailAdapter;

            }
        }
        /// <summary>
        /// 查询采购收货入库主表
        /// </summary>
        /// <returns></returns>

        public purchase.cgshrkMasterDataTable getcgshrkMasterAll()
        {
            try
            {
                return cgshrkMasterAdapter.GetDatacgshrkMasterAll ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询采购收货入库从表
        /// </summary>
        /// <returns></returns>

        public purchase.cgshrkDetailDataTable getcgshrkDetailAll()
        {
            try
            {
                return cgshrkDetailAdapter.GetDatacgshrkDetailALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询采购收货入库从表
        /// </summary>
        /// <returns></returns>

        public baobiaoDataSet.V_cgshrkDetailDataTable getV_cgshrkDetailBystocknameNo()
        {
            try
            {
                return V_cgshrkDetailAdapter.GetDataV_cgshrkDetailBystocknameNo();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询采购收货入库从表
        /// </summary>
        /// <returns></returns>

        public baobiaoDataSet.V_cgshrkDetailDataTable getV_cgshrkDetailBystockname()
        {
            try
            {
                return V_cgshrkDetailAdapter.GetDataV_cgshrkDetailBystockname ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询采购收货入库从表
        /// </summary>
        /// <returns></returns>

        public baobiaoDataSet.V_cgshrkDetailDataTable getV_cgshrkDetailBymatNamelike1(string matName)
        {
            try
            {
                return V_cgshrkDetailAdapter.GetDataV_cgshrkDetailBymatNamelike1(matName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询采购收货入库从表
        /// </summary>
        /// <returns></returns>

        public baobiaoDataSet.V_cgshrkDetailDataTable getV_cgshrkDetailBymatNamelikeNo(string matName)
        {
            try
            {
                return V_cgshrkDetailAdapter.GetDataV_cgshrkDetailBymatNamelikeNo (matName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据入库单号查询采购收货入库表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public purchase.cgshrkMasterDataTable getcgshrkMasterAllBybillNo(string billNo)
        {
            try
            {
                return cgshrkMasterAdapter.GetDatacgshrkMasterBybillNo(billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据入库单号查询采购收货入库从表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public purchase.cgshrkDetailDataTable getscgshrkDetailAllBybillNo(string billNo)
        {
            try
            {
                return cgshrkDetailAdapter.GetDatacgshrkDetailBybillNo(billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 修改采购收货入库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int cgshrkInsert(purchase .cgshrkMasterDataTable masterTable, purchase.cgshrkDetailDataTable pTable, string state, string billNo, string Date)
        {
          //  cgshrkDetailModel cgshrkDetailRow = new cgshrkDetailModel();
           int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                cgshrkMasterDAL cgshrkMaster = new cgshrkMasterDAL(tn, conn);
                cgshrkDetailDAL cgshrkDetail = new cgshrkDetailDAL(tn, conn);
                cgshrkMaster.cgshrkMasterInsertII(masterTable , pTable);
                //basListDAL basList = new basListDAL(tn, conn);
                //if (state == "新增")
                //{

                //    purchase.cgshrkMasterDataTable cgddTable = getcgshrkMasterAllBybillNo(billNo);
                //    if (cgddTable.Rows.Count == 0)
                //    {
                //        cgshrkMaster.cgshrkMasterInsert(row);
                //    }
                //    else
                //    {
                //        row.billNo = "R" + Date + "-" + basicSql.getOrderSerail("采购收货入库单号");
                //        cgshrkMaster.cgshrkMasterInsert(row);
                //    }

                //    //更新单据编号的过程 
                //    string orderSerial = basicSql.getOrderSerail("采购收货入库单号");
                //    basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "采购收货入库单号");
                //}
                //if (state == "编辑")
                //{
                //    cgshrkMaster.cgshrkMasterUpdate(row);
                //}


                //foreach (purchase.cgshrkDetailRow detailRow in pTable)
                //{
                //    if (detailRow.RowState != DataRowState.Deleted)
                //    {
                //        cgshrkDetailRow.recordid = detailRow.recordid;
                //        cgshrkDetailRow.billNo = detailRow.billNo;
                //        cgshrkDetailRow.matNum = detailRow.matNum;
                //        cgshrkDetailRow.matSpec = detailRow.matSpec;
                //        cgshrkDetailRow.qty = detailRow.qty;
                //        cgshrkDetailRow.price = detailRow.price;
                //        cgshrkDetailRow.currency = detailRow.currency;
                //        cgshrkDetailRow.unit = detailRow.unit;
                //        cgshrkDetailRow.fqty = detailRow.fqty;
                //        cgshrkDetailRow.funit = detailRow.funit;
                //        cgshrkDetailRow.createDate = DateTime.Now;
                //        if (!detailRow.IsmarkNull())
                //        {
                //            cgshrkDetailRow.mark = detailRow.mark;
                //        }
                //        else
                //        {
                //            cgshrkDetailRow.mark = "";
                //        }
                //    }

                //    //编辑子表记录,新增
                //    if (detailRow.RowState == DataRowState.Added)
                //    {

                //        cgshrkDetail.cgshrkDetailInsert(cgshrkDetailRow);


                //    }
                //    //修改
                //    if (detailRow.RowState == DataRowState.Modified)
                //    {
                //        cgshrkDetail.cgshrkDetailUpdate(cgshrkDetailRow);
                //    }
                //    //删除
                //    if (detailRow.RowState == DataRowState.Deleted)
                //    {
                //        DataRow delrow = (DataRow)detailRow;
                //        cgshrkDetail.cgshrkDetailDel(delrow["recordid", DataRowVersion.Original].ToString());
                //    }


                //}

                tn.Commit();
                return i;

            }
            //catch (SqlException ex)
            //{
            //    tn.Rollback();
            //    if (ex.Number == 2627)
            //    {
            //        throw new Exception("这个入库单号已经存在");
            //    }


            //    throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            //}
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }


        /// 删除一行采购收货入库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int cgshrkMasterDel(string billNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                cgshrkMasterDAL cgshrkMaster = new cgshrkMasterDAL(tn, conn);
                cgshrkMaster.cgshrkDel (billNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }

    }
}
